package com.barryredmond.javamelibchecker;

import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;

public class JsrCheckerForm extends Form implements CommandListener{

    private final Command aboutCommand;
    private final Command exitCommand;
    private final Command checkClassExists;
    
    public JsrCheckerForm() {
        super("Supported APIs");
        
        this.addCommand(aboutCommand = new Command("About", Command.ITEM, 1));
        this.addCommand(exitCommand = new Command("QUIT", Command.EXIT, 2));
        this.addCommand(checkClassExists = new Command( "Check Another Class", Command.SCREEN, 3));
        this.setCommandListener(this);

        Vector/*CheckableJSR*/ itemsToCheck = new Vector();
        fillVectorWithItemsToCheck( itemsToCheck );
        
        StringItem title = new StringItem("Supported", null);
        this.append(title);
        // Append all supported APIs
        this.appendAll( itemsToCheck, true );
        
        
        
        title = new StringItem("NOT Supported", null);
        this.append(title);
        // Append all unsupported APIs.
        this.appendAll( itemsToCheck, false );
        
    }

    private void fillVectorWithItemsToCheck(Vector itemsToCheck) {
        itemsToCheck.addElement( new SupportableJSR( "Connected Limited Device Configuration(CLDC) 1.0 ", SupportableJSR.TRUE ) );
        itemsToCheck.addElement( new SupportableJSR( "Connected Limited Device Configuration(CLDC) 1.1", "java.lang.ref.WeakReference" ) );
        itemsToCheck.addElement( new SupportableJSR( "Mobile Information Device Profile(MIDP) 1.0", SupportableJSR.TRUE ) );
        itemsToCheck.addElement( new SupportableJSR( "Mobile Information Device Profile(MIDP) 2.0", "javax.microedition.lcdui.game.GameCanvas" ) );
        
        itemsToCheck.addElement( new SupportableJSR( "JSR 66: RMI", "java.rmi.RMISecurityManager" ) );
        itemsToCheck.addElement( new SupportableJSR( "jsr75pim", "javax.microedition.pim.PIM" ) );
        itemsToCheck.addElement( new SupportableJSR( "jsr75file", "javax.microedition.io.file.FileSystemRegistry" ) );
        itemsToCheck.addElement( new SupportableJSR( "JSR 82: Java APIs for Bluetooth", "javax.bluetooth.LocalDevice" ) );
        
        itemsToCheck.addElement( new SupportableJSR( "jsr120", "javax.wireless.messaging.TextMessage" ) );
        itemsToCheck.addElement( new SupportableJSR( "JSR 135: Mobile Media API 1.2", "javax.microedition.media.Player" ) );
        itemsToCheck.addElement( new SupportableJSR( "JSR 172: J2ME Web Services Specification", "javax.xml.parsers.SAXParser" ) );
        itemsToCheck.addElement( new SupportableJSR( "JSR 177: Security and Trust Services APIs", "javax.microedition.pki.UserCredentialManager" ) );
        
        itemsToCheck.addElement( new SupportableJSR( "JSR 209: Advanced Graphics and User Interface", "javax.microedition.agui.event.DeviceKeyEvent" ) );
        itemsToCheck.addElement( new SupportableJSR( "JSR 211: Content Handler API", "javax.microedition.content.ContentHandler" ) );
        itemsToCheck.addElement( new SupportableJSR( "JSR 219: Security", "javax.security.auth.x500.X500Principal" ) );        
        itemsToCheck.addElement( new SupportableJSR( "JSR 239: Java Binding for the OpenGL® ES", "javax.microedition.khronos.opengles.GL11ExtensionPack") );
        
    }

    private void appendAll( Vector itemsToCheck, boolean supported ){
        
        for( int i = 0; i < itemsToCheck.size(); i++ ){
            final SupportableJSR currentItem = (SupportableJSR) itemsToCheck.elementAt( i );
            
            if( currentItem.isSupported != null && currentItem.isSupported.booleanValue() == supported){
                append( new StringItem(null, currentItem.friendlyName ) );
            }
        }
        
    }

    public void commandAction(Command command, Displayable disp) {
        if(disp == this){
            if(command == aboutCommand){
                Display.getDisplay(LibCheckerMIDlet.getMIDlet()).setCurrent(new AboutScreen(this));
            }else if(command == exitCommand){
                LibCheckerMIDlet.getMIDlet().notifyDestroyed();
            }else if( command == checkClassExists ){
            	Display.getDisplay( LibCheckerMIDlet.getMIDlet()).setCurrent( getUserInputClassChecker() );
            }
        }
        
    }

    private UserInputClassChecker userInputClassChecker;
	private Displayable getUserInputClassChecker() {
		if( userInputClassChecker==null ){
			userInputClassChecker = new UserInputClassChecker( this );
		}
		return userInputClassChecker;
	}

}
